<?php

namespace App\Http\Controllers\Api;

use App\Http\Resources\ClubResource;
use App\Http\Resources\CommentResource;
use App\Http\Resources\ReportResource;
use App\Http\Resources\TechnicianResource;
use App\Models\Forum;
use App\Repositories\TopicRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\CommentRequest;
use App\Repositories\CommentRepository;
use App\Models\Topic;
use App\Models\Comment;
use App\Transformers\CommentTransformer;

class CommentController extends Controller
{
    protected $comment;
    
    public function __construct(CommentRepository $comment)
    {
        $this->comment = $comment;
    }
    /**
     * 评论详情
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $rootId = $request->get('root_id', 0);
        $page = $request->get('page', 1);
        $includeRootComment = $request->get('include_root_comment', 0);
        $result = (new TopicRepository())->commentDetailData($rootId, $page);
        $comments = $result['comments'];
        $resource = CommentResource::collection($comments);

        //当第一页时，同时返回根评论
        if ($rootId && $page <= 1 && $includeRootComment) {
            $user = auth_user();
            $rootComment = Comment::query()->with(['user', 'detail'])->findOrFail($rootId);
            $isLiked = $user->likeComments()->where('target_id', $rootId)->exists();
            $rootComment->is_liked = intval($isLiked);
            $resource->additional(['root' => new CommentResource($rootComment)]);
        }

        return api(RET_OK, MSG_OK, $resource);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CommentRequest $request)
    {
        $pid = $request->get('pid', 0);
        $result = $this->comment->create($request);
        if ($result['ret'] !== 0) {
            return $result;
        }
        //立即返回列表，只包含当前最新的这条评论
        $topic = $result['data']['topic'];
        $comment = $result['data']['comment'];

        if ($pid)
        {
            $parentComment = Comment::query()->findOrFail($pid);
            $rootId = $parentComment->root_id ? $parentComment->root_id : $parentComment->id;
            $result = (new TopicRepository())->commentDetailData($rootId, 1, $comment->id);
            $comments = $result['comments'];
        }
        else
        {
            $result = (new TopicRepository())->topicDetailData($topic->id, 1, $comment->id);
            $comments = $result['comments'];
        }
        $resource = CommentResource::collection($comments);
        return api(RET_OK, MSG_OK, $resource);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $with = ['user', 'detail', 'detail.attachments'];
        $comment = Comment::with($with)->where('key', $id)->firstOrFail();
        $commentApiData = fractal()
            ->item($comment)
            ->transformWith(new CommentTransformer())
            ->parseIncludes($with)
            ->toArray();

//        dd($commentApiData);

        return normalize(0, 'OK', $commentApiData['data']);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function my(Request $request)
    {
        $with =  [
            'user', 'detail',
            'topic', 'topic.user', 'topic.mainFloor', 'topic.mainFloor.detail', 'topic.mainFloor.detail.attachments'
        ];
        $page = $request->page;
        $user = auth_user();
        $options = [
            'with' => $with,
            'uid' => $user->id,
            'page' => $page,
            'order_by' => ['id' => 'desc'],
        ];
        $comments = $this->comment->getList($options);
        $resource = CommentResource::collection($comments);

        return api(RET_OK, MSG_OK, $resource);
    }

}
